from functools import wraps

from flask_restful import current_app

from extensions import db


def with_session(func):

    @wraps(func)
    def set_trx(*args, **kwargs):
        db.session.begin(subtransactions=True)
        try:
            ret = func(*args, **kwargs)
            db.session.commit()
            return ret
        except Exception as ex:
            current_app.logger.error(ex, exc_info=1)
            current_app.logger.error(">>>>>>>>>>>>>>>>>with_session.db.session.rollback<<<<<<<<<<<<<<<<<<<<<<<")
            db.session.rollback()
            raise
    return set_trx
